<?php
class staff extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览管理员 ','2');
		$da = $this -> pub -> get_setting('管理员', 'Administrator');
		$this -> load -> view('adminx/staff', $da);
	}

	function add() {
		$this -> pub -> logp('添加管理员 ','2');
		if ($this -> session -> userdata('state') != 2) {
			redirect('/sgl');
		}
		$this -> pub -> logx('创建管理员');
		$da['name'] = $this -> input -> post('name');
		$da['password'] = md5($this -> input -> post('password'));
		$da['phone'] = $this -> input -> post('phone');
		$da['note'] = $this -> input -> post('note');
		//1普通管理员 2 高级管理员
		$da['state'] = $this -> input -> post('state');
		$da['type'] = $this -> input -> post('type');
		$da['ly'] = $this -> input -> post('ly');

		$da['jg_name'] = $this -> input -> post('jg_name');
		$da['jg_qy'] = $this -> input -> post('jg_qy');
		$da['jg_dz'] = $this -> input -> post('jg_dz');
		$da['jg_phone'] = $this -> input -> post('jg_phone');

		$da['jg_pic'] = $this -> input -> post('jg_pic');
		$da['jg_sort'] = $this -> input -> post('jg_sort');

		$da['jg_jy'] = $this -> input -> post('jg_jy');

		$da['jg_dianzhao'] = $this -> input -> post('jg_dianzhao');

		if ($da['jg_qy']) {
			$jwd = $this -> pub -> dt_jwd($da['jg_qy'] . ' ' . $da['jg_dz']);
			$da['jg_jd'] = $jwd['lat'];
			$da['jg_wd'] = $jwd['lng'];
		} else {
			$da['jg_jd'] = $this -> input -> post('jg_jd');
			$da['jg_wd'] = $this -> input -> post('jg_wd');
		}

		$da['mid'] = $this -> input -> post('mid');
		$da['is_xs'] = $this -> input -> post('is_xs');
		$this -> pub -> insert('staff', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('修改管理员 -'.$this -> input -> post('idx'),'2');
		if ($this -> session -> userdata('state') != 2) {
			redirect('/sgl');
		}
		$this -> pub -> logx('管理员修改');
		$da['name'] = $this -> input -> post('name');
		if ($this -> input -> post('password') != '******') {
			$da['password'] = md5($this -> input -> post('password'));
		}
		$da['phone'] = $this -> input -> post('phone');
		$da['note'] = $this -> input -> post('note');

		$da['state'] = $this -> input -> post('state');
		$da['type'] = $this -> input -> post('type');
		$da['ly'] = $this -> input -> post('ly');

		$da['jg_name'] = $this -> input -> post('jg_name');
		$da['jg_qy'] = $this -> input -> post('jg_qy');
		$da['jg_dz'] = $this -> input -> post('jg_dz');
		$da['jg_phone'] = $this -> input -> post('jg_phone');
		if ($this -> input -> post('jg_pic')) {
			$da['jg_pic'] = $this -> input -> post('jg_pic');
		}

		$da['jg_sort'] = $this -> input -> post('jg_sort');
		$da['jg_jy'] = $this -> input -> post('jg_jy');

		$da['jg_dianzhao'] = $this -> input -> post('jg_dianzhao');

		if ($da['jg_qy']) {
			$jwd = $this -> pub -> dt_jwd($da['jg_qy'] . ' ' . $da['jg_dz']);
			$da['jg_jd'] = $jwd['lat'];
			$da['jg_wd'] = $jwd['lng'];
		} else {
			$da['jg_jd'] = $this -> input -> post('jg_jd');
			$da['jg_wd'] = $this -> input -> post('jg_wd');
		}

		$da['mid'] = $this -> input -> post('mid');

		$da['is_xs'] = $this -> input -> post('is_xs');
		$this -> db -> update('staff', $da, array('staff_id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取管理员信息 -'.$this -> input -> post('idx'),'2');
		if ($this -> session -> userdata('state') != 2) {
			redirect('/sgl');
		}
		$q = $this -> db -> get_where('staff', array('staff_id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da= $q -> row_array(0);
			$da['typex'] = 1;
			$da['jg_picx'] = $this -> pub -> oss_dz($da['m']['jg_pic']);
			$da['password']='******';
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function del() {
		$this -> pub -> logp('删除管理员 -'.$this -> input -> post('idx'),'2');
		if ($this -> session -> userdata('state') != 2) {
			redirect('/sgl');
		}
		$this -> pub -> logx('栏目删除');
		$da['type'] = 1;
		$this -> db -> update('staff', array('staff_del' => 2), array('staff_id' => $this -> input -> post('idx')));
		echo json_encode($da);
	}

	function get() {
		$this -> pub -> logp('获取管理员信息','2');
		if ($this -> session -> userdata('state') != 2) {
			redirect('/sgl');
		}
		$this -> db -> select('count(*)');
		if ($this -> input -> post('state')) {
			$this -> db -> where('pan_staff.state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('key')) {
			$this -> db -> where('(pan_staff.name like \'%' . $this -> input -> post('key') . '%\' or pan_staff.jg_name like \'%' . $this -> input -> post('key') . '%\' )');
		}
		$this -> db -> where('staff_del', 1);

		$q = $this -> db -> get_where('staff');
		$ls = $q -> row_array(0);
		$num = $ls['count(*)'];

		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> where('staff_del', 1);
		$this -> db -> select('pan_staff.*,pan_member.nickname,pan_member.headimgurl');
		$this -> db -> join('member', 'pan_member.mid=pan_staff.mid', 'left');
		$this -> db -> order_by('jg_sort', 'asc');
		if ($this -> input -> post('key')) {
			$this -> db -> where('(pan_staff.name like \'%' . $this -> input -> post('key') . '%\' or pan_staff.jg_name like \'%' . $this -> input -> post('key') . '%\' )');
		}
		if ($this -> input -> post('state')) {
			$this -> db -> where('pan_staff.state', $this -> input -> post('state'));
		}
		$q = $this -> db -> get_where('staff');
		$da['db'] = $this -> db -> last_query();
		$da['content'] = '';
		$type[1] = '允许登录';
		$type[2] = '临时强迫退出';
		$type[3] = '禁用';

		$state[1] = '普通管理员';
		$state[2] = '高级管理员';
		$state[3] = '机构管理员';
		$state[4] = '等待审核';

		$is_xs[1] = '列表显示';
		$is_xs[2] = '列表不显示';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>'.$v['staff_id'].'</td>';
			$da['content'] .= '<td><a target="_blank" href="/sgl/tx/' . $v['mid'] . '"><img width="48" src="' . ($v['headimgurl'] ? $v['headimgurl'] : '/mex/lance/img/tx.jpg') . '" /><br><img width="48" src="' . $this -> pub -> oss_dz($v['jg_pic']) . '" /></a></td>';
			$da['content'] .= '<td>' . $v['name'] . ' <br>昵称：' . $v['nickname'] . '<br>' . $v['phone'] . '</td>';

			if ($v['jg_jy']) {
				$qq = $this -> db -> get_where('fl', array('fl_id' => $v['jg_jy']));
				$ls = $qq -> row_array(0);
				$jg_jy = '<br>' . $ls['name'];
			} else {
				$jg_jy = '';
			}

			$da['content'] .= '<td><a href="/sgl/ys/' . $v['staff_id'] . '">' . $state[$v['state']] . '</a><br>' . $v['jg_name'] . '<br>' . $type[$v['type']] . $jg_jy . '<br>' . $is_xs[$v['is_xs']] . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['staff_id'] . '\')" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</button> <button onclick="get_ewm(\'' . $v['staff_id'] . '\',\'' . $v['jg_name'] . '\',4)" type="button" class="btn blue"><i class="fa fa-qrcode"></i> 二维码</button>  <button onclick="del(\'' . $v['staff_id'] . '\',\''.$v['staff_name'].'\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>